﻿

Class Main

    Public Shared _rightWindowContent As String
    Private uc As UserControl
    Public Shared _pageIsValid As Boolean = True

    Public Shared Property PageIsValid() As String
        Get
            Return _pageIsValid
        End Get
        Set(ByVal Value As String)
            _pageIsValid = Value
        End Set
    End Property

    Public Shared ReadOnly Property RightWindowContent() As String
        Get
            Return _rightWindowContent
        End Get
    End Property

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        DisplayLoginScreen()
        Dim x As New Expander
        x.Name = "default"
        expanderDefaults(x)
        expanderSubLedger.IsExpanded = True

    End Sub
    Private Sub DisplayLoginScreen()
        Dim frm As New frmLogin()

        frm.Owner = Me
        frm.ShowDialog()
        If frm.DialogResult.HasValue And frm.DialogResult.Value Then
            MessageBox.Show("User Logged In")
        Else
            Me.Close()
        End If
    End Sub


    Private Sub expanderDefaults(ByVal expander As Expander)
        Select Case expander.Name
            Case "expanderAdministration"
                expanderDataForms.IsExpanded = False
                expanderItems.IsExpanded = False
                expanderReports.IsExpanded = False
                expanderSubLedger.IsExpanded = False
                expanderSynch.IsExpanded = False
            Case "expanderDataForms"
                expanderAdministration.IsExpanded = False
                expanderItems.IsExpanded = False
                expanderReports.IsExpanded = False
                expanderSubLedger.IsExpanded = False
                expanderSynch.IsExpanded = False
            Case "expanderItems"
                expanderAdministration.IsExpanded = False
                expanderDataForms.IsExpanded = False
                expanderReports.IsExpanded = False
                expanderSubLedger.IsExpanded = False
                expanderSynch.IsExpanded = False
            Case "expanderReports"
                expanderAdministration.IsExpanded = False
                expanderDataForms.IsExpanded = False
                expanderItems.IsExpanded = False
                expanderSubLedger.IsExpanded = False
                expanderSynch.IsExpanded = False
            Case "expanderSubLedger"
                expanderAdministration.IsExpanded = False
                expanderDataForms.IsExpanded = False
                expanderItems.IsExpanded = False
                expanderReports.IsExpanded = False
                expanderSynch.IsExpanded = False
            Case "expanderSynch"
                expanderAdministration.IsExpanded = False
                expanderDataForms.IsExpanded = False
                expanderItems.IsExpanded = False
                expanderReports.IsExpanded = False
                expanderSubLedger.IsExpanded = False
            Case Else
                expanderAdministration.IsExpanded = False
                expanderDataForms.IsExpanded = False
                expanderItems.IsExpanded = False
                expanderReports.IsExpanded = False
                expanderSubLedger.IsExpanded = True
                expanderSynch.IsExpanded = False
        End Select


    End Sub

    Private Sub treeviewAdministration_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles treeviewAdministration.SelectedItemChanged

        GridMainRight.Children.Remove(uc)


        Dim treeviewSelectedNode As String = treeviewAdministration.SelectedValue.name
        _rightWindowContent = treeviewSelectedNode
        Select Case True
            Case treeviewSelectedNode Like "*Users*"
                uc = New ucUsers
            Case treeviewSelectedNode Like "*Accounts*"
                uc = New ucUsers
            Case Else
                uc = New ucGeneralSettings
        End Select

        GridMainRight.Children.Add(uc)
    End Sub

    Private Sub expanderAdministration_Expanded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles expanderAdministration.Expanded
        expanderDefaults(sender)
    End Sub


    Private Sub expanderDataForms_Expanded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles expanderDataForms.Expanded
        expanderDefaults(sender)
    End Sub

    Private Sub expanderItems_Expanded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles expanderItems.Expanded
        expanderDefaults(sender)
        If _pageIsValid Then
            GridMainRight.Children.Remove(uc)

            uc = New ucItems
            GridMainRight.Children.Add(uc)
        Else
            Dim warningString As String = "Some mandatory fields are missing."
            System.Windows.MessageBox.Show(warningString, "Missing entry", MessageBoxButton.OK, MessageBoxImage.Information)
        End If
    End Sub

    Private Sub expanderReports_Expanded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles expanderReports.Expanded
        expanderDefaults(sender)
    End Sub

    Private Sub expanderSubLedger_Expanded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles expanderSubLedger.Expanded
        expanderDefaults(sender)
    End Sub

    Private Sub expanderSynch_Expanded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles expanderSynch.Expanded
        expanderDefaults(sender)
        GridMainRight.Children.Remove(uc)

        uc = New ucSynchronise
        GridMainRight.Children.Add(uc)
    End Sub

    'cancel treeview selection if pageisvalid is false
    'this ensure that entries are saved before leaving the current form
    Private Sub treeviewDataForms_PreviewMouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles treeviewDataForms.PreviewMouseDown
        If _pageIsValid Then
            e.Handled = False
        Else
            Dim warningString As String = "There are unsaved entries. Please save your entries first."
            System.Windows.MessageBox.Show(warningString, "Entry error", MessageBoxButton.OK, MessageBoxImage.Information)
        End If

    End Sub

    Private Sub treeviewDataForms_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles treeviewDataForms.SelectedItemChanged
        If _pageIsValid Then
            GridMainRight.Children.Remove(uc)

            Dim treeviewSelectedNode As String = treeviewDataForms.SelectedValue.name
            _rightWindowContent = treeviewSelectedNode
            Select Case True
                Case treeviewSelectedNode Like "*Category*"
                    uc = New ucCategories
                Case treeviewSelectedNode Like "*Location*"
                    uc = New ucLocations
                Case treeviewSelectedNode Like "*Person*"
                    uc = New ucPersons
                Case treeviewSelectedNode Like "*Department*"
                    uc = New ucDepartments
                Case Else
                    uc = New ucCategories
            End Select

            GridMainRight.Children.Add(uc)
        Else
            Dim warningString As String = "Some mandatory fields are missing."
            System.Windows.MessageBox.Show(warningString, "Missing entry", MessageBoxButton.OK, MessageBoxImage.Information)
        End If

    End Sub

    Private Sub Items_Selected(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Items.Selected

    End Sub

    Private Sub treeviewSubLedger_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles treeviewSubLedger.SelectedItemChanged
        If _pageIsValid Then

            GridMainRight.Children.Remove(uc)

            Dim treeviewSelectedNode As String = treeviewSubLedger.SelectedValue.name
            _rightWindowContent = treeviewSelectedNode
            Select Case True
                Case treeviewSelectedNode Like "*Location*"
                    uc = New ucLocationLedger
                Case treeviewSelectedNode Like "*Department*"
                    uc = New ucDepartmentLedger
                Case treeviewSelectedNode Like "*AssetClassification*"
                    uc = New ucCategoryLedger
                Case treeviewSelectedNode Like "*LeasedAsset*"
                    uc = New ucLeasedAssetLedger
                Case Else
                    uc = New ucDepartmentLedger
            End Select
            GridMainRight.Children.Add(uc)
        Else
            Dim warningString As String = "Some mandatory fields are missing."
            System.Windows.MessageBox.Show(warningString, "Missing entry", MessageBoxButton.OK, MessageBoxImage.Information)
        End If



    End Sub

    Private Sub treeviewReports_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles treeviewReports.SelectedItemChanged
        If _pageIsValid Then
            GridMainRight.Children.Remove(uc)
            uc = New ucReport
            GridMainRight.Children.Add(uc)
        Else
            Dim warningString As String = "Some mandatory fields are missing."
            System.Windows.MessageBox.Show(warningString, "Missing entry", MessageBoxButton.OK, MessageBoxImage.Information)
        End If
    End Sub
End Class

Public Class TreeViewLineConverter
    Implements IValueConverter
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Dim item As TreeViewItem = DirectCast(value, TreeViewItem)
        Dim ic As ItemsControl = ItemsControl.ItemsControlFromItemContainer(item)
        Return (ic.ItemContainerGenerator.IndexFromContainer(item) = ic.Items.Count - 1)
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw (New Exception("The method or operation is not implemented."))
    End Function
End Class

